//------------------------------------------------------------------------------
// <pcdd-entitiy>
//     彩票主流业务类库
//     新闻业务对象
// </pcdd-entitiy>
//------------------------------------------------------------------------------ 

using CL.Entity.Json.WebAPI;
using CL.Enum.Common;
using CL.Game.DAL;
using CL.Game.Entity;
using CL.Json.Entity;
using CL.Redis.BLL;
using CL.Tools.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CL.Game.BLL
{

    /// <summary>
    /// NewsBLL 业务
    /// </summary>
    public class NewsBLL
    {
        NewsDAL dal = new NewsDAL(DbConnectionEnum.CaileGame);


        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int NewsID)
        {
            return dal.Exists(NewsID);
        }
        /// <summary>
        /// 插入对象
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int InsertEntity(NewsEntity entity)
        {
            return dal.InsertEntity(entity);
        }
        /// <summary>
        /// 修改对象
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int ModifyEntity(NewsEntity entity)
        {
            return dal.ModifyEntity(entity);
        }
        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="NewsID"></param>
        /// <returns></returns>
        public bool DelEntity(int NewsID)
        {
            return dal.DelEntity(NewsID);
        }
        /// <summary>
        /// 查询对象
        /// </summary>
        /// <param name="NewsID"></param>
        /// <returns></returns>
        public NewsEntity QueryEntity(int NewsID)
        {
            return dal.QueryEntity(NewsID);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public List<udv_NewsTypeName> QueryListByPage(string strTitle, int TypeID, int pageSize, int pageIndex, ref int recordCount)
        {
            return dal.QueryListByPage(strTitle, TypeID, pageSize, pageIndex, ref recordCount);
        }
        /// <summary>
        /// 分页获取数据列表(审核)
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public List<udv_NewsTypeName> QueryListByPage(int pageSize, int pageIndex, ref int recordCount)
        {
            return dal.QueryListByPage(pageSize, pageIndex, ref recordCount);
        }
        /// <summary>
        /// 查询最新N条新闻
        /// </summary>
        /// <param name="NewsID"></param>
        /// <param name="IsGreater"></param>
        /// <returns></returns>
        public List<NewsEntity> QueryEntitys(int NewsID, int Top, bool IsGreater)
        {
            return dal.QueryEntitys(NewsID, Top, IsGreater);
        }
        /// <summary>
        /// 查询最新N条新闻
        /// </summary>
        /// <param name="TypeID">类型ID</param>
        /// <param name="LotteryCode">彩种ID</param>
        /// <param name="Top">查询多少条</param>
        /// <returns></returns>
        public List<NewsEntity> QueryEntitys(int TypeID, int LotteryCode, int Top)
        {
            return dal.QueryEntitys(TypeID, LotteryCode, Top);
        }
        /// <summary>
        /// 客户端分析资讯
        /// </summary>
        /// <param name="NewsID">新闻ID</param>
        /// <returns></returns>
        public ClientNews QueryClientNews(int NewsID)
        {
            ClientNews RecEntity = new BusinessRedis().QueryClientNewsRedis(NewsID);
            string LookNewsUrl = ConfigHelper.GetConfigString("LOOKNEWSURL");
            int LookTopNews = 3;
            int.TryParse(System.Configuration.ConfigurationManager.AppSettings["LOOKTOPNEWS"], out LookTopNews);
            NewsEntity Entity = null;
            if (RecEntity == null)
            {
                Entity = dal.Get(NewsID);
                if (Entity != null)
                {
                    List<NewsEntity> Entitys = null;
                    List<NewsTitle> Data = null;
                    Entitys = this.QueryEntitys(NewsID, LookTopNews, true);
                    if (Entitys == null || Entitys.Count < LookTopNews)
                    {
                        var Replenish = this.QueryEntitys(NewsID, LookTopNews - (Entitys == null ? 0 : Entitys.Count), false);
                        if (Replenish != null && Replenish.Count > 0)
                        {
                            if (Entitys == null)
                                Entitys = new List<NewsEntity>();
                            Entitys.AddRange(Replenish);
                        }
                    }
                    if (Entitys != null && Entitys.Count > 0)
                    {
                        Data = Entitys.Select(s => new NewsTitle
                        {
                            ID = s.NewsID,
                            IsRecommend = s.IsRecommend,
                            Title = s.Title.Length > 10 ? s.Title.Substring(0, 10) : s.Title,
                            LookUrl = string.Format("{0}?id={1}", LookNewsUrl, s.NewsID),
                            Time = s.PublishTime.ToString("yyyyMMddHHmmss")
                        }).ToList();
                        if (RecEntity == null)
                            RecEntity = new ClientNews();
                        RecEntity.Author = Entity.Author;
                        RecEntity.LotNumber = Entity.LotNumber;
                        RecEntity.NewsData = Data;
                        RecEntity.OpposeNum = Entity.OpposeNum;
                        RecEntity.ReadNum = Entity.ReadNum;
                        RecEntity.SupportNum = Entity.SupportNum;
                        RecEntity.Time = Entity.PublishTime.ToString("yyyy-MM-dd HH:mm");
                        RecEntity.Title = Entity.Title;
                    }
                    new BusinessRedis().SaveClientNewsRedis(NewsID, RecEntity);
                }
            }
            Task.Factory.StartNew(() =>
            {
                if (RecEntity != null)
                {
                    RecEntity.ReadNum += 1;
                    new BusinessRedis().SaveClientNewsRedis(NewsID, RecEntity);
                }
                if (Entity == null)
                    Entity = dal.Get(NewsID);
                if (Entity != null)
                {
                    Entity.ReadNum += 1;
                    dal.Update(Entity);
                }
            });
            return RecEntity;
        }
        /// <summary>
        /// 查询彩种分析
        /// </summary>
        /// <param name="LotteryCode"></param>
        /// <returns></returns>
        public List<NewsTitle> QueryLotAnalysis(int LotteryCode)
        {
            List<NewsTitle> Data = new BusinessRedis().QueryLotAnalysisRedis(LotteryCode);
            if (Data == null || Data.Count == 0)
            {
                int LotNewsTop = ConfigHelper.GetConfigInt("LOTNEWSTOP");
                int NewsTypeID = ConfigHelper.GetConfigInt("NEWSTYPEID");
                string LookNewsUrl = ConfigHelper.GetConfigString("LOOKNEWSURL");
                var Entitys = this.QueryEntitys(NewsTypeID, LotteryCode, LotNewsTop);
                if (Entitys != null && Entitys.Count > 0)
                {
                    Data = Entitys.Select(s => new NewsTitle()
                    {
                        ID = s.NewsID,
                        Title = s.Title.Length > 10 ? s.Title.Substring(0, 10) : s.Title,
                        IsRecommend = s.IsRecommend,
                        LookUrl = string.Format("{0}?id={1}", LookNewsUrl, s.NewsID),
                        Time = s.PublishTime.ToString("yyyyMMddHHmmss")
                    }).ToList();
                    //本地缓存
                    new BusinessRedis().SaveLotAnalysisRedis(LotteryCode, Data);
                }
            }
            return Data;
        }
        /// <summary>
        /// 支持文章
        /// </summary>
        /// <param name="NewsID"></param>
        /// <returns></returns>
        public bool SupportNews(int NewsID)
        {
            return dal.SupportNews(NewsID);
        }
        /// <summary>
        /// 反对文章
        /// </summary>
        /// <param name="NewsID"></param>
        /// <returns></returns>
        public bool OpposeNews(int NewsID)
        {
            return dal.OpposeNews(NewsID);
        }


        #region 自定义
        /// <summary>
        /// 查询彩种新闻列表
        /// </summary>
        /// <param name="LotteryCode"></param>
        /// <returns></returns>
        public NewsTitleResult LotAnalysis(int LotteryCode)
        {
            int RecCode = (int)ResultCode.Success;
            List<NewsTitle> Data = null;
            try
            {
                Data = this.QueryLotAnalysis(LotteryCode);
            }
            catch (Exception ex)
            {
                new Log("NewsBLL").Write(string.Format("查询彩种新闻列表错误：{0}", ex.StackTrace), true);
                RecCode = (int)ResultCode.RequestHasFailed;
            }
            return new NewsTitleResult()
            {
                Code = RecCode,
                Msg = Common.GetDescription((ResultCode)RecCode),
                Data = Data
            };
        }
        /// <summary>
        /// 客户端查看新闻
        /// </summary>
        /// <param name="NewsID"></param>
        /// <returns></returns>
        public ClientNewsResult LookNewsAnalysis(int NewsID)
        {
            int RecCode = (int)ResultCode.Success;
            ClientNews Data = null;
            try
            {
                Data = this.QueryClientNews(NewsID);
                if (Data == null)
                    RecCode = (int)ResultCode.NullData;
            }
            catch (Exception ex)
            {
                new Log("NewsBLL").Write(string.Format("客户端查看新闻错误：{0}", ex.StackTrace), true);
                RecCode = (int)ResultCode.RequestHasFailed;
            }
            return new ClientNewsResult()
            {
                Code = RecCode,
                Msg = Common.GetDescription((ResultCode)RecCode),
                Data = RecCode != (int)ResultCode.Success ? null : Data
            };
        }

        /// <summary>
        /// 评价文章
        /// </summary>
        /// <param name="NewsID">文章ID</param>
        /// <param name="IsSupport">是否支持：1.表示 其他表示反对如：0</param>
        /// <returns></returns>
        public JsonResult EvaluateNews(int NewsID, int IsSupport)
        {
            int RecCode = (int)ResultCode.Success;
            try
            {
                var Entity = new BusinessRedis().QueryClientNewsRedis(NewsID);
                if (IsSupport == 1)
                {
                    Entity.SupportNum += 1;
                    this.SupportNews(NewsID);
                }
                else
                {
                    Entity.OpposeNum += 1;
                    this.OpposeNews(NewsID);
                }
                new BusinessRedis().SaveClientNewsRedis(NewsID, Entity);
            }
            catch (Exception ex)
            {
                new Log("NewsBLL").Write(string.Format("评价文章错误：{0}", ex.StackTrace), true);
                RecCode = (int)ResultCode.RequestHasFailed;
            }
            return new JsonResult()
            {
                Code = RecCode,
                Msg = Common.GetDescription((ResultCode)RecCode)
            };
        }
        #endregion
    }
}
